<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Deal extends CI_Controller {


	function __construct()
    {     	
    	parent::__construct();
    	if(get_cookie('id')) {
    		$this->session->set_userdata('id', get_cookie('id'));
			$this->session->set_userdata('avatar', get_cookie('avatar'));
			$this->session->set_userdata('name', get_cookie('name'));
    	}
		$this->load->model('category'); 
		$this->load->model('user'); 
		$this->load->model('coupon'); 
		$this->load->model('adeal'); 
		$this->load->model('product'); 
		$this->load->model('comment'); 
    }


    public function index() {	
    	$data["categories"] = $this->category->getPageCategories();
		$data["session_id"] = $this->session->userdata('id');
		$data["session_name"] = $this->session->userdata('name');
		$data["session_avatar"] = $this->session->userdata('avatar');

		$this->load->view('deal.php', $data);
	}

	public function id() {
		$data["categories"] = $this->category->getPageCategories();
		$data["session_id"] = $this->session->userdata('id');
		$data["session_name"] = $this->session->userdata('name');
		$data["session_avatar"] = $this->session->userdata('avatar');
		$data["images"] = $this->adeal->getImagesById(func_get_arg(0));

		$row = $this->adeal->getRowById(func_get_arg(0));
		$data["comments"] = $this->comment->getCommentsByDealId(func_get_arg(0));
		$data["adeal"] = $row[0];
		$data["products"] = $this->product->getProductsByDealId($row[0]->deal_id);
		$this->load->view('deal.php', $data);
	}

	public function comment() {
		$param = func_get_arg(0);
		$this->comment->addComment($_POST['text'], $param, $this->session->userdata('name'), $this->session->userdata('id'));
		header("Location: /godeal/deal/id/".$param);
	}
}